<?php
	//error_reporting(0);
	set_time_limit(0);
	include_once 'simplehtmldom/simple_html_dom.php';
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	//$data = '#在抖音，记录美好生活#【库存】抛个硬币 如果摔碎了 今天我就不吃饭了 #校园生活  #大学 https://v.douyin.com/p9taJa/ 复制此链接，打开【抖音短视频】，直接观看视频！';
	$data = '#在抖音，记录美好生活#【库存】抛个硬币 如果摔碎了 今天我就不吃饭了 #校园生活  #大学 https://v.douyin.com/pVh8BH/ 复制此链接，打开【抖音短视频】，直接观看视频！';
	$data = getData($data);
	echo json_encode($data);
	function getData($data){
		$url = getUrl($data);
		$cookie_jar = dirname(__FILE__).'/tmp.txt';//tempnam('./tmp','cookie');
		$data = get_content($url, $cookie_jar);
		$page = str_get_html($data);
		$data = array(
				'base'=>array(
					'headimg'=>false, // 头像
					'name'=>false, // 昵称
					'title'=>false, // 标题（姑且叫标题吧）
					'description'=>false // 描述
				),
				'video'=>array(
					'cover'=>false, // 封面
					'src'=>false, // 路径
					'width'=>false, // 宽度
					'height'=>false // 高度
				)
			);
		$user = $page->find('div[class=user-info]');
		// 头像、 昵称
		if(count($user) > 0){
			$img = $user[0]->find('div[class=avatar]');
			if(count($img) > 0){
				$img = $img[0]->find('img');
				if(count($img) > 0){
					// 头像
					$data['base']['headimg'] = $img[0]->src;
					// 昵称
					$data['base']['name'] = $img[0]->alt;
				}
			}
		}
		// 标题、描述
		$title = $page->find('div[class=challenge-info]');
		if(count($title) > 0){
			$description = $title[0]->next_sibling();
			$title = $title[0]->first_child()->first_child();
			$data['base']['title'] = $title->innertext;
			$data['base']['description'] = $description->innertext;
		}
		$video = $page->find('div[id=pageletReflowVideo]');
		if(count($video) > 0){
			$script = $video[0]->next_sibling();
			if(!empty($script)){
				$script = $script->next_sibling();
				if(!empty($script)){
					$script = $script->next_sibling()->innertext;
					$data['video'] = getVideo($script);
				}
			}
			
			
		}
		return $data;
	}
	
	function getVideo($scripts){
		$video = array();
		$scripts = preg_replace('/\s+/','',$scripts);
		// 宽度
		preg_match('/videoWidth:([0-9.]*),/' , $scripts, $matches);
		if(empty($matches) || count($matches) < 2){
			$video['width'] = false;
		}else{
			$video['width'] = $matches[1];
		}
		// 高度
		preg_match('/videoHeight:([0-9.]*),/' , $scripts, $matches);
		if(empty($matches) || count($matches) < 2){
			$video['height'] = false;
		}else{
			$video['height'] = $matches[1];
		}
		// 视频路径
		preg_match('/playAddr:"(.*)",/' , $scripts, $matches);
		if(empty($matches) || count($matches) < 2){
			$video['src'] = false;
		}else{
			$video['src'] = $matches[1];
		}
		// 封面
		preg_match('/cover:"(.*)"}/' , $scripts, $matches);
		if(empty($matches) || count($matches) < 2){
			$video['cover'] = false;
		}else{
			$video['cover'] = $matches[1];
		}
		return $video;
	}
	function get_content($url, $cookie,$referfer='') {
	//var_dump($post);exit;
	$useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
	/*if ($curl_loops++ >= $curl_max_loops) {
		$curl_loops = 0;
		return false;
	}*/
	if($referfer == ''){
		$referfer = 'https://www.kujiale.com/';
	}
	$header = array("Referer: ".$referfer); 
	  $curl = curl_init();//初始化curl模块 
	  curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址 
	  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); //不验证证书
	  curl_setopt($curl, CURLOPT_HEADER, 1);//是否显示头信息 
	  curl_setopt($curl, CURLOPT_HTTPHEADER,$header); 
	  //curl_setopt ($curl,CURLOPT_REFERER,'http://www.kujiale.com/');
	  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//是否自动显示返回的信息 
	  curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); //设置Cookie信息保存在指定的文件中 
	  curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中 
	  //curl_setopt($curl, CURLOPT_POST, 1);//post方式提交 
	  //curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息 
	  //curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
	  
	  curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
	  //curl_setopt($curl, CURLOPT_REFERER, 'http://www.kujiale.com/');
	  $data = curl_exec($curl);//执行cURL 
	  $ret = $data;
	  list($header, $data) = explode("\r\n\r\n", $data, 2);
	  $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
	  $last_url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
	  //var_dump($output);
	  $httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE); 
	  //var_dump($httpCode);
	  //echo '<hr/>';
	  curl_close($curl);//关闭cURL资源，并且释放系统资源 
	  if ($http_code == 301 || $http_code == 302) {
	  	  $matches = array();
	  	  preg_match('/Location:(.*?)\n/', $header, $matches);
	  	  $url = @parse_url(trim(array_pop($matches)));
	  	  if (!$url) {
	  	  	  $curl_loops = 0;
	  	  	  return $data;
	  	  }
	  	  $new_url = $url['scheme'] . '://' . $url['host'] . $url['path']
	  	  	  . (isset($url['query']) ? '?' . $url['query'] : '');
	  	  $new_url = stripslashes($new_url);
	  	  return get_content($new_url,$cookie);
	 } else {
	 	 $curl_loops = 0;
	 	 list($header, $data) = explode("\r\n\r\n", $ret, 2);
	 	 return $data;
	 }
} 
	function getUrl($data){
		preg_match('/https:\/\/v.douyin.com\/.*\//' , $data, $matches);
		if(empty($matches) || count($matches) != 1){
			return false;
		}else{
			return $matches[0];
		}
	}
?>